package com.csu.jpetstore_reconfiguration_back.Controller;

import com.csu.jpetstore_reconfiguration_back.common.CommonResponse;
import com.csu.jpetstore_reconfiguration_back.domain.Category;
import com.csu.jpetstore_reconfiguration_back.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class CategoryController {
    @Autowired
    CategoryService categoryService;

    //获取所有分类
    @GetMapping("/categories")
    @ResponseBody
    public CommonResponse<List<Category>> getAllCategory(){
        return categoryService.getAllCategory();
    }

    //获取单个分类
    @GetMapping("/categories/{id}")
    @ResponseBody
    public CommonResponse<Category> getCategory(@PathVariable ("id")String categoryId){
        return categoryService.getCategory(categoryId);
    }

    //添加分类
    @PostMapping("/categories")
    @ResponseBody
    public CommonResponse<Category> addCategory(@Validated @RequestBody Category category){
        return categoryService.addCategory(category);
    }

    //修改分类
    @PostMapping("/categories/{id}")
    @ResponseBody
    public CommonResponse<Category> updateCategory(@PathVariable ("id")String categoryId,@Validated @RequestBody Category category){
        return categoryService.updateCategory(categoryId, category);
    }

    //删除分类
    @PostMapping("/categories/{id}/delete")
    @ResponseBody
    public CommonResponse<Category> deleteCategory(@PathVariable ("id")String categoryId){
        return categoryService.deleteCategory(categoryId);
    }

}
